using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager m_graphicsManager;

        private Matrix World;
        private Matrix View;
        private Matrix Projection;
        private Matrix SecondWorld;

        private Effect m_effect;
        private Effect m_secondEffect;
        private Model m_modelTentancle;
        private Model m_modelSphere;
        private double m_fov;
        private Vector4 m_color;
        private bool m_reachedZero;

        // Effect paramters.
        private EffectParameter m_colorParameter;
        
        public Game1()
        {
            m_graphicsManager = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            m_color = new Vector4(0, 0, 1, 1);
            m_fov = 70.0;
            //m_reachedZero = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            m_modelTentancle = Content.Load<Model>("TentacleThing");
            m_modelSphere = Content.Load<Model>("Sphere");
            m_effect = Content.Load<Effect>("Effect1");
            m_secondEffect = Content.Load<Effect>("Effect2");
            
            RemapModel(m_modelTentancle, m_effect);
            m_effect.CurrentTechnique = m_effect.Techniques["Technique1"];

            RemapModel(m_modelSphere, m_secondEffect);
            m_secondEffect.CurrentTechnique = m_secondEffect.Techniques["Technique2"];
            
            // Set the effect parameters.
            m_colorParameter = m_effect.Parameters["modelColor"];
        }

        public static void RemapModel(Model model, Effect effect)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = effect;
                }
            }
        }


        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here

            // Colors for the model in the format RGBA
            //if (m_reachedZero == false)
            //{
            //    m_color.X = m_color.X - 0.01f;
            //}

            //if (m_color.X < 0)
            //{
            //    m_reachedZero = true;
            //}

            //if (m_reachedZero == true)
            //{
            //    m_color.X = m_color.X + 0.01f;
            //}

            //if (m_color.X > 1)
            //{
            //    m_reachedZero = false;
            //}

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // Effect1
            World = Matrix.CreateRotationY((float)gameTime.TotalGameTime.Ticks * 1E-7f) * 
                Matrix.CreateRotationX((float)gameTime.TotalGameTime.Ticks * 1E-7f) * 
                Matrix.CreateScale(3.0f) * Matrix.CreateTranslation(0.0f, 0.0f, -2.0f);

            //View = Matrix.CreateTranslation(new Vector3(0, 0, -50));
            View = Matrix.CreateLookAt(new Vector3(0, 5, -50), Vector3.Zero, Vector3.Up);
            Projection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI * m_fov / 180.0), GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);

            // Colors for the model in the format RGBA
            m_colorParameter.SetValue(m_color);

            m_effect.Parameters["World"].SetValue(World);
            m_effect.Parameters["View"].SetValue(View);
            m_effect.Parameters["Projection"].SetValue(Projection);

            m_effect.Parameters["lightPos"].SetValue(new Vector3(40, 0, -10));
            m_effect.Parameters["secondLightPos"].SetValue(new Vector3(-40, 0, -10));

            DrawMesh(m_modelTentancle);

            // Effect2
            SecondWorld = Matrix.CreateTranslation(40, 0, -10);
            m_secondEffect.Parameters["World"].SetValue(SecondWorld);
            m_secondEffect.Parameters["View"].SetValue(View);
            m_secondEffect.Parameters["Projection"].SetValue(Projection);
            DrawMesh(m_modelSphere);

            SecondWorld = Matrix.CreateTranslation(-40, 0, -10);
            m_secondEffect.Parameters["World"].SetValue(SecondWorld);
            DrawMesh(m_modelSphere);
        }

        private void DrawMesh(Model a_model)
        {
            foreach (ModelMesh mesh in a_model.Meshes)
            {
                foreach (Effect e in mesh.Effects)
                {
                    mesh.Draw();
                }
            }
        }
    }
}
